<?php 
class AdminNotice extends Module{
	function AdminNotice($row){
		Module::Module($row);
		$act = Url::get("act");
		$cmd = Url::get("cmd");
		if($act=="user" && User::have_permit(ADMIN_NOTICE_USER)){
			switch($cmd){				
				case 'delete':
					$id = (int)Url::get('id');
					$this->delNoticeUser($id);
					Url::redirect_current(array('act'));
					break;
				case "add":	
				case "edit":					
					require_once 'forms/EditAdminNoticeUser.php';
					$this->add_form(new EditAdminNoticeUserForm());					
					break;
				default:
					require_once 'forms/AdminNoticeUser.php';
					$this->add_form(new AdminNoticeUserForm());
					break;
			}
		}		
		elseif(User::have_permit(ADMIN_NOTICE)){					
			switch($cmd){				
				case 'delete':
					$id = (int)Url::get('id');
					$this->delNotice($id);
					Url::redirect_current();
					break;
				case "add":	
				case "edit":					
					require_once 'forms/EditAdminNotice.php';
					$this->add_form(new EditAdminNoticeForm());					
					break;
				default:
					require_once 'forms/AdminNotice.php';
					$this->add_form(new AdminNoticeForm());
					break;
			}						
		}
		else{
			Url::access_denied();
		}
	}
	
	function delNotice($id){
		if($id){
			User::getAdminNotice(0,1);
			return DB::query("DELETE FROM admin_notice WHERE id IN($id)");
		}
	}
	
	function delNoticeUser($id){
		if($id){
			$re = DB::query("SELECT user_id FROM admin_notice_user WHERE id IN($id) ORDER BY id DESC LIMIT 1");
			$user_ids = "";
			while($row = mysql_fetch_assoc($re)){
				$user_ids .= ($user_ids?",":"").$row["user_id"];
			}
			
			if($user_ids){
				$arr_ids  = explode(",",$user_ids);
				foreach($arr_ids as $user_id){
					User::getAdminNoticeUser($user_id,0,1);
				}
			}
						
			return DB::query("DELETE FROM admin_notice_user WHERE id IN($id)");
		}
	}
}
?>